#include "../Utility/httplib.h"
#include "Calculate.hpp"

using namespace ns_Calculate;
using namespace httplib;
using namespace std;

void Usage(std::string proc)
{
    std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }

    Server svr;

    svr.Get("/hello", [](const Request &req, Response &resp)
            { resp.set_content("nice to meet you too!", "text/plain;charset=utf-8"); });

    svr.Post("/TropoCorrect", [](const Request &req, Response &resp)
             {
        std::string inJson = req.body;
        std::string outJson;
        if(Calculate::GetTropoCorrect(inJson,&outJson))
            resp.set_content(outJson,"application/json;charset=utf-8"); 
        else 
            resp.set_content("Failed!","text/plain;charset=utf-8"); });

    svr.Post("/IonoCorrect", [](const Request &req, Response &resp)
             {
        std::string inJson = req.body;
        std::string outJson;
        if(Calculate::GetIonoCorrect(inJson,&outJson))
            resp.set_content(outJson,"application/json;charset=utf-8"); 
        else 
            resp.set_content("Failed!","text/plain;charset=utf-8"); });

    svr.Post("/Physics", [](const Request &req, Response &resp)
             {
        std::string inJson = req.body;
        std::string outJson;
        if(Calculate::GetPhysics(inJson,&outJson))
            resp.set_content(outJson,"application/json;charset=utf-8"); 
        else 
            resp.set_content("Failed!","text/plain;charset=utf-8"); });

    svr.Post("/Position", [](const Request &req, Response &resp)
             {
        std::string inJson = req.body;
        std::string outJson;
        if(Calculate::GetSinglePointPositon(inJson,&outJson))
            resp.set_content(outJson,"application/json;charset=utf-8"); 
        else 
            resp.set_content("Position Failed!","text/plain;charset=utf-8"); });

    svr.Post("/Velocity", [](const Request &req, Response &resp)
             {
        std::string inJson = req.body;
        std::string outJson;
        if(Calculate::GetSinglePointVelocity(inJson,&outJson))
            resp.set_content(outJson,"application/json;charset=utf-8"); 
        else 
            resp.set_content("Velocity Failed!","text/plain;charset=utf-8"); });

    svr.listen("0.0.0.0", atoi(argv[1]));

    return 0;
}